package mobile.ui.cliente;

import mobile.dominio.Cliente;
import mobile.dominio.Zona;
import mobile.persistencia.PCliente;
import mobile.persistencia.PZona;
import mobile.ui.LaAnfitriona;
import moio.util.ArrayList;
import moio.util.Iterator;
import waba.fx.Color;
import waba.sys.Convert;
import waba.sys.Settings;
import waba.ui.*;

public class SeleccionarCliente extends Window{
	private Button btnAtras, btnBuscar, btnSeleccionar;
	private Grid grilla;
	private Edit txtNombre;
	private ComboBox cmbzona;
	public Cliente clienteSeleccionado;

	public SeleccionarCliente(){
		Button.commonGap = 1;

		add(new Label("Nombre: "), LEFT + 1, TOP + 2);
		add(txtNombre = new Edit(""),AFTER,SAME);

		add(new Label("Zona: "), LEFT + 1, AFTER);
		ArrayList colZonas = PZona.instance.todasLasZonas();
		Zona unaZona = new Zona();
		unaZona.setDescripcion("Todas");
		unaZona.setId(0);
		colZonas.add(0,unaZona);
		add(cmbzona = new ComboBox(colZonas.toArray()));
		cmbzona.setRect(AFTER, SAME + 2, FILL, PREFERRED);
		cmbzona.select(unaZona);

		add(btnBuscar = new Button("Buscar"));
		btnBuscar.setRect(LEFT, AFTER + 2, PREFERRED, PREFERRED + 1);
		add(btnAtras = new Button("  Atras  "), RIGHT - 1, BOTTOM - 1);
		add(btnSeleccionar = new Button(" Seleccionar "), BEFORE - 2, SAME);

		int gridWidths[] = { 
				fm.getTextWidth("xxx"), // id
				fm.getTextWidth("xxxxxxxxxxxxx"), // Nombre
				//fm.getTextWidth("xxxxxxxxxx"), // Zona
				fm.getTextWidth("xxx"), // saldo
		};
		int gridAligns[] = { LEFT, LEFT, LEFT, RIGHT };
		String[] gridCaptions = {"Cod.", "Nombre", /*"Zona",*/ "Saldo"};

		grilla = new Grid(gridCaptions, gridWidths, gridAligns, false);
		add(grilla);
		grilla.verticalLineStyle = Grid.VERT_DOT;
		grilla.setRect(LEFT, AFTER + 5, FILL, FIT, btnBuscar);

		if (Settings.isColor)
		{
			Color clrBack = LaAnfitriona.defaultBackColor;
			Color clrFore = LaAnfitriona.defaultForeColor;
			btnBuscar.setBackForeColors(clrBack,clrFore);
			btnAtras.setBackForeColors(clrBack,clrFore);
			btnSeleccionar.setBackForeColors(clrBack,clrFore);
			grilla.setBackColor(Color.WHITE);
		}
		Button.commonGap = 0;
	}

	public void update(){
		Zona unaZona = null;
		String strnombre = txtNombre.getText();
		
		if (cmbzona.getSelectedItem() != null && cmbzona.getSelectedItem().toString() != ""){
			unaZona = (Zona) cmbzona.getSelectedItem();
		}
		
		ArrayList colClientes = PCliente.instance.buscarClientes(strnombre);
		int count = colClientes.size();
		if (count == 0){
			grilla.clear();
		}else{
			String[][] s = new String[count][];

			Iterator it = colClientes.iterator();
			int i = 0;
			while(it.hasNext()){
				Cliente c = (Cliente)it.next();
				s[i] = new String[] {Convert.toString(c.getId()), c.getNombre(), /*c.getZona().getDescripcion(),*/ Convert.toString(c.getSaldo())};
				i++;
			}
			grilla.setItems(s);               
		}
	}

	private Cliente getSelectedCustomer(){
		int idx = grilla.getSelectedRow();
		if (idx < 0) return null;
		String []s = grilla.getItem(idx);
		return PCliente.instance.obtenerClienteXID(Convert.toInt(s[0]));
	}
	

	public void onEvent(Event e)
	{
		switch (e.type)
		{
		case ControlEvent.PRESSED:
			if (e.target == btnAtras){
				unpop();
			} else if (e.target == btnBuscar){
				update();
			} else if (e.target == btnSeleccionar){
				clienteSeleccionado = getSelectedCustomer();
				if (clienteSeleccionado != null){
					unpop();
				}
			}
			break;
		}
	}

	public void onAdd()
	{
		update();
	}
}
